package org.sucri.floxs.servlet;

import org.apache.commons.collections15.map.MultiKeyMap;
import org.apache.commons.collections15.keyvalue.MultiKey;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.ServletException;
import javax.servlet.ServletConfig;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import java.io.IOException;

/**
 * 
 http://www.javaworld.com/jw-09-1999/jw-09-servlet.html?page=3

 * User: Wen Yu
 * Date: Jul 31, 2007
 * Time: 7:36:26 PM
 * To change this template use File | Settings | File Templates.
 */
public class FrontServlet extends HttpServlet
{
  protected String mHtmlTemplatePath = "C:\\Apps\\projects\\floxs\\FigureEight\\resources\\css\\fruit";
  protected MultiKeyMap<String, RequestHandler> _Handlers = new MultiKeyMap<String, RequestHandler>();
  protected EntityManagerFactory emf = null;

  public void init(ServletConfig config) throws ServletException
  {
    super.init(config);
    emf = Persistence.createEntityManagerFactory("vpp");

    //mHtmlTemplatePath = this.getServletContext().getRealPath("/css/fruit/");
    mHtmlTemplatePath = this.getServletContext().getRealPath("/css/fluxs/");

    System.out.println("EntityManagerFactory created");
  }

   public void destroy()
   {
     //First: release resources and clean up
     super.destroy();
     emf.close();
   }

   public void doPost(HttpServletRequest  req,
                      HttpServletResponse res)
       throws ServletException, IOException
   {
     process(req, res, false);
   }

   public void doGet(HttpServletRequest req, HttpServletResponse res)
        throws ServletException, IOException
   {
     process(req, res, true);
   }

  protected boolean process(HttpServletRequest req,
                            HttpServletResponse res, boolean isGet)
        throws ServletException, IOException
  {
    HttpSession session = req.getSession(true);
    String action = req.getParameter("action");
    String page_uid = req.getParameter("page");

    User user = (User)session.getAttribute("user");

    RequestHandler handler = getRequestHandler(action);

    return handler != null ? handler.processRequest(emf, user, req, res) : false;
  }
  private RequestHandler getRequestHandler(String action)
  {
    return  _Handlers != null ? _Handlers.get(new MultiKey<String>("action=" + action)) : null;
  }
  /*
  protected void generateDWwrJS() throws Exception
  {
    ServletContext servletContext = new FakeServletContext();
    ServletConfig  servletConfig  = new FakeServletConfig("dwr-test", servletContext);

// Setup the DWR container
    DefaultContainer container = ContainerUtil.createDefaultContainer(servletConfig);
    ContainerUtil.setupDefaultContainer(container, servletConfig);

    WebContextFactory.WebContextBuilder webContextBuilder = StartupUtil.initWebContext(servletConfig, servletContext, container);
    StartupUtil.initServerContext(servletConfig, servletContext, container);
    ContainerUtil.prepareForWebContextFilter(servletContext, servletConfig, container, webContextBuilder, null);

    ContainerUtil.configureFromSystemDwrXml(container);

    DwrXmlConfigurator local = new DwrXmlConfigurator();
    local.setClassResourceName("/dwr.xml");
    local.configure(container);

    ContainerUtil.configureFromInitParams(container, servletConfig);

    ContainerUtil.publishContainer(container, servletConfig);
    webContextBuilder.set(null, null, servletConfig, servletContext, container);

    Remoter remoter = (Remoter) container.getBean(Remoter.class.getName());
    String script = remoter.generateInterfaceScript("<ExportedClass>", "<Path>");
    System.out.println(script);
  }*/
}